package cn.bdqn.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.bdqn.bean.Detail;
import cn.bdqn.util.DataSourceUitls;
import cn.bdqn.util.Page;

public class NewsDaoImpl extends BaseDaoImpl implements NewsDao {
	@Override
	public List<Detail> findAll() {
		return this.findAll("select * from news_detail ", Detail.class);
	}

	@Override
	public int save(Detail detail) {

		String sql = "	insert into `news_detail`" + "  ( "
				+ "   `categoryId`,     " + "   `title`,          "
				+ "   `summary`,        " + "   `content`,        "
				+ "   `picPath`,        " + "   `author`,         "
				+ "   `createDate`      " + "    )     "
				+ "values (    ?,?,?,?,?,?,?)";
		Object[] objs = { detail.getCategoryId(), detail.getTitle(),
				detail.getSummary(), detail.getContent(), detail.getPicPath(),
				detail.getAuthor(), detail.getCreateDate() };
		return super.executeUpdate(sql, objs);

	}

	@Override
	public void findByPage(Page page, Detail detail) {
		String sql = "select * from news_detail";
		String where = " where 1=1 ";
		List parms = new ArrayList();
		if (detail != null) {
			if (detail.getTitle() != null
					&& detail.getTitle().trim().length() > 0) {
				where += " and title like ?";
				parms.add("%" + detail.getTitle() + "%");
			}
		}
		sql = sql + where;
		super.execQuery(page, sql, parms);
	}

	@Override
	public int del(Long id) {
		String sql = " delete  FROM  `news_detail` WHERE `id` = ?;";
		Object[] objs = { id };
		return super.executeUpdate(sql, objs);

	}

}
